﻿using System;
using System.Text;

namespace InfoJetSoft.Service.Util
{
	
	public class TypeFormat
	{
		public string Type {
			get {
				return this.type;
			}
		}
		public string NumDigits
		{
			get
			{
				return this.numDigits;
			}
			
		}
		public string NegativeOrder
		{
			get
			{
				return this.negativeOrder;
			}
			
		}
		public string PositiveOrder
		{
			get
			{
				return this.positiveOrder;
			}
			
		}
		public string CurrencyLocale
		{
			get
			{
				return this.currencyLocale;
			}
			
		}
		public string Grouping
		{
			get
			{
				return this.grouping;
			}
			
		}
		public string Locale
		{
			get
			{
				return this.locale;
			}
			
		}
		public string DateFormat
		{
			get
			{
				return this.dateFormat;
			}
			
		}
		public string TimeFormat
		{
			get
			{
				return this.timeFormat;
			}
			
		}
		public string NoSeconds
		{
			get
			{
				return this.noSeconds;
			}
			
		}
		public string Percentage
		{
			get
			{
				return this.percentage;
			}
			
			set
			{
				this.percentage = value;
			}
			
		}
		
		private string numDigits = null;
		private string negativeOrder = null;
		private string positiveOrder = null;
		private string currencyLocale = null;
		private string percentage = null;
		private string grouping = null;
		private string locale = null;
		private string dateFormat = null;
		private string timeFormat = null;
		private string noSeconds = null;
		private string type = null;
		
		public TypeFormat(string type, string format)
		{
			this.type = type;
			if( type.Equals( "percentage" ) ){
				this.percentage = "1";
			}
			StringTokenizer tokenizer = new StringTokenizer(format, ";");
			while (tokenizer.HasMoreTokens())
			{
				string facet = tokenizer.NextToken();
				facet = facet.Trim();
				if (facet.Length > 0)
				{
					int index = facet.IndexOf(":");
					if (index > 0)
					{
						string name = facet.Substring(0, (index) - (0));
						string value_Renamed = facet.Substring(index + 1, (facet.Length) - (index + 1));
						if (name.Equals("numDigits"))
						{
							this.numDigits = value_Renamed.Trim();
						}
						else if (name.Equals("negativeOrder"))
						{
							this.negativeOrder = value_Renamed.Trim();
						}
						else if (name.Equals("positiveOrder"))
						{
							this.positiveOrder = value_Renamed.Trim();
						}
						else if (name.Equals("currencyLocale"))
						{
							this.currencyLocale = value_Renamed.Trim();
						}
						else if (name.Equals("grouping"))
						{
							this.grouping = value_Renamed.Trim();
						}
						else if (name.Equals("locale"))
						{
							this.locale = value_Renamed.Trim();
						}
						else if (name.Equals("dateFormat"))
						{
							this.dateFormat = convertDateFormat(value_Renamed.Trim());
						}
						else if (name.Equals("timeFormat"))
						{
							this.timeFormat = convertTimeFormat(value_Renamed.Trim());
						}
						else if (name.Equals("noSeconds"))
						{
							this.noSeconds = value_Renamed.Trim();
						}
					}
				}
			}
		}
		
		private static string convertDateFormat(string dateFormat)
		{
			if (dateFormat.Equals(""))
			{
				return "yyyy-M-d";
			}
			if (dateFormat.Equals("Short Date"))
			{
				return System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern;
			}
			if (dateFormat.Equals("Long Date"))
			{
				return System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.LongDatePattern;
			}
			if (dateFormat.Equals("Year Month")){
				return System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.YearMonthPattern;
			}
			if (dateFormat.Equals("none")){
				return "";
			}
			StringBuilder formatBuffer = new StringBuilder(dateFormat);
			return formatBuffer.ToString();
		}
		
		private static string convertTimeFormat(string dateFormat)
		{
			if (dateFormat.Equals(""))
			{
				return "H:mm:ss";
			}
			if (dateFormat.Equals("none")){
				return "";
			}
			StringBuilder formatBuffer = new StringBuilder(dateFormat);
			return formatBuffer.ToString();
		}
		
		private static void  replaceSymbol(StringBuilder formatBuffer, string symbolStr, char symbolChar, string replaceStr)
		{
			int length = symbolStr.Length;
			int index = formatBuffer.ToString().IndexOf(symbolStr);
			while (index >= 0)
			{
				for (int i = 0; true; i++)
				{
					if (index + length + i < formatBuffer.Length)
					{
						if (formatBuffer[index + length + i] != symbolChar)
						{
							formatBuffer.Replace(formatBuffer.ToString(index, index + length + i - index), replaceStr, index, index + length + i - index);
							break;
						}
					}
					else
					{
						formatBuffer.Replace(formatBuffer.ToString(index, index + length - index), replaceStr, index, index + length - index);
						break;
					}
				}
				index = formatBuffer.ToString().IndexOf(symbolStr);
			}
		}
		
		[STAThread]
		public static void  Main(string[] args)
		{
			System.Console.Out.WriteLine(convertDateFormat("ddd aaa tt"));
		}
	}
}